package org.sjs.dreamfly.app

import com.google.common.eventbus.EventBus
import org.sjs.dreamfly.browser.WebLocation
import org.sjs.dreamfly.model.MovieInfo
import org.sjs.dreamfly.model.VideoInfo
import org.sjs.dreamfly.common.util.JsonUtil
import org.sjs.dreamfly.model.ixigua.iXiGuaFilterResponse


val defaultEventBus = EventBus()

class PlayListLoadedEvent(val location: WebLocation?, list: List<VideoInfo>) {
    var playList: MutableList<VideoInfo> = mutableListOf()
    init {
        if (list.isNotEmpty()) {
            playList.addAll(list)
            list.forEach {
                it.refer?.split("-")?.let {list ->
                    if (list.size >= 3) {
                        it.sid = list[1].toInt()
                        it.nid = list[2].replace(".html", "").toInt()
                    }
                }
            }
        }
    }

    override fun toString(): String {
        return "PlayListLoadedEvent(playList=$playList)"
    }

}

class PlayInfoLoadedEvent(val videoInfo: VideoInfo, private val from: Int = 0) {
    companion object {
        const val FROM_JS = 0
        const val FROM_NATIVE = 1
    }

    override fun toString(): String {
        return "PlayInfoLoadedEvent(from=$from, videoInfo=$videoInfo)"
    }
}

class MovieListLoadedEvent {
    var movieList: MutableList<MovieInfo> = mutableListOf()
    var response: iXiGuaFilterResponse? = null
    var location: WebLocation? = null
    constructor(location: WebLocation?, list: List<MovieInfo>) {
        this.location = location
        if (list.isNotEmpty()) {
            movieList.addAll(list)
        }
    }

    constructor(location: WebLocation?, response: iXiGuaFilterResponse) {
        this.location = location
        this.response = response
        response.data?.albums?.forEach {
            movieList.add(it.toMovieInfo())
        }
    }
}

class SearchListLoadedEvent(list: List<MovieInfo>) {
    var movieList: MutableList<MovieInfo> = mutableListOf()
    init {
        if (list.isNotEmpty()) {
            movieList.addAll(list)
        }
    }
}